#include <stdio.h>
#include "MenuElement.h"
#include "GL/glut.h"

MenuElement::MenuElement(TextureManager * textureManager, char * textureName, Vector2D position, Vector2D size, Vector2D * textureVertices)
{
	this->textureManager = textureManager;
	
	textureIndex = -1;
	if(textureManager)
	{
		if(!textureManager->LoadTexture(textureName))
		{
			printf("Failed to load texture %s\n", textureName);
		}
		else
			textureIndex = textureManager->TextureIndex(textureName);
	}
	
	this->position = position;
	this->size = size;
	this->textureVertices = textureVertices;
	this->gameManager = NULL;
	this->clickAction = NULL;
	hasFocus = false;
	label = NULL;
}
		
MenuElement::MenuElement(TextureManager * textureManager, char * textureName, Vector2D position, Vector2D size, Vector2D * textureVertices, GameManager * gameManager, void (GameManager::*clickAction)(void))
{
	this->textureManager = textureManager;
	
	textureIndex = -1;
	if(textureManager)
	{
		if(!textureManager->LoadTexture(textureName))
		{
			printf("Failed to load texture %s\n", textureName);
		}
		else
			textureIndex = textureManager->TextureIndex(textureName);
	}
	
	this->position = position;
	this->size = size;
	this->textureVertices = textureVertices;
	this->gameManager = gameManager;
	this->clickAction = clickAction;
	this->label = NULL;
}
MenuElement::MenuElement(char * string, TextureManager * textureManager, char * textureName, Vector2D position, Vector2D size, Vector2D * textureVertices, GameManager * gameManager, void (GameManager::*clickAction)(void))
{
	this->textureManager = textureManager;
	
	textureIndex = -1;
	if(textureManager)
	{
		if(!textureManager->LoadTexture(textureName))
		{
			printf("Failed to load texture %s\n", textureName);
		}
		else
			textureIndex = textureManager->TextureIndex(textureName);
	}
	
	this->position = position;
	this->size = size;
	this->textureVertices = textureVertices;
	this->gameManager = gameManager;
	this->clickAction = clickAction;
	this->label = NULL;
	if(strlen(string)>0)
	{
		float size1 = (size.x * 0.9)/strlen(string);
		float size2 = size.y * 0.75;
		
		if(size2 < size1)
			size1 = size2;
		Vector2D stringPosition = Vector2D(position.x + 0.5 * (size.x-size1*strlen(string)),
										   position.y + 0.5 * (size.y - size1));
		this->label = new PrintString(textureManager, string, stringPosition, size1);
	}
}

MenuElement::~MenuElement()
{
	if(textureVertices)
		delete(textureVertices);
	textureVertices = NULL;
	if(label)
		delete(label);
	label = NULL;
}

void MenuElement::Draw(Vector2D parentPosition)
{
	if(label)
	{
		label->Draw(parentPosition);
	}
	
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	glOrtho(0,100,100,0, -1, 1);
	
	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();
	glTranslatef(0, 0, -0.5);
	
	//printf("textureIndex = %d\n", textureIndex);
	if(textureIndex >= 0)
	{
		textureManager->EnableTexture(textureIndex);
		glEnable(GL_TEXTURE_2D);
		glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
		glBlendFunc(GL_ONE, GL_ONE);
		glAlphaFunc( GL_GREATER, 0.01 );
  		glEnable( GL_ALPHA_TEST );
  		glEnable(GL_BLEND);
	}
	else
		printf("No texture index fool\n");
	
	glColor3f(1,1,1);	
	glBegin(GL_QUADS);
	
	Vector2D vertices[4];
	vertices[0] = parentPosition + position;
	vertices[1] = Vector2D(vertices[0].x + size.x, vertices[0].y);
	vertices[2] = Vector2D(vertices[1].x, vertices[1].y + size.y);
	vertices[3] = Vector2D(vertices[0].x, vertices[2].y);
	
	for(int i = 0; i < 4; i++)
	{
		//printf("drawing element %d at\n\t(%f, %f)\n",i, vertices[i].x, vertices[i].y);
		glTexCoord2f(textureVertices[i].x, textureVertices[i].y);
		glVertex3f(vertices[i].x, vertices[i].y, 1);
	}
	glEnd();
	
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	glPopMatrix();
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_BLEND);
	glMatrixMode(GL_PROJECTION);
	glPopMatrix();
}

bool MenuElement::Click(Vector2D parentPosition, Vector2D mousePosition)
{
	if(!gameManager)
		return false;
		
	mousePosition.y = 100 - mousePosition.y;
	
	Vector2D topLeft = (parentPosition + position);	
	Vector2D bottomRight = topLeft + size;//Vector2D(parentPosition.x + position.x + size.x, parentPosition.y + position.y + size.y);
	
	printf("Checking click\n\ttopLeft=(%f,%f)\n\tbottomRight=(%f,%f)\n\tmouse=(%f,%f)\n",
		topLeft.x, topLeft.y, bottomRight.x, bottomRight.y, mousePosition.x, mousePosition.y);
	if(mousePosition.x >= topLeft.x && mousePosition.x <= bottomRight.x &&
	   mousePosition.y <= bottomRight.y && mousePosition.y >= topLeft.y)
	{
		(gameManager->*clickAction)();
		return true;
	}	
	return false;
}

bool MenuElement::HandleUserInput(bool enterKeyDepressed)
{
	if(hasFocus)
	{
		(gameManager->*clickAction)();
		return true;	
	}
	return false;	
}
